<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas</title>
  <style>
    canvas {
      box-shadow: 5px 5px 10px #CCC;
    }
  </style>
</head>

<body>
  <canvas id="canvas" width="500" height="500">
    你的浏览器太low了~
  </canvas>
  <script>

    window.onload = function () {

      var canvas = document.getElementById('canvas')
      if (canvas.getContext) {
        var ctx = canvas.getContext('2d');
        // createLinearGradient(起点x, 起点y, 终点x, 终点y):坐标的位置值针对canvas画布而言；起点终点的位置主要是定渐变方向
        var linegrad = ctx.createLinearGradient(0, 0, 0, 150); // 如果改成createLinearGradient(0, 0, 150, 150)，则会是像右下45度方向渐变

        // addColorStop(参数一:值范围0-1，表示颜色所在的相对值, 参数二： 颜色)
        linegrad.addColorStop(0, 'pink');
        linegrad.addColorStop(0.5, '#FFF');
        linegrad.addColorStop(1, 'green');

        var linegrad2 = ctx.createLinearGradient(0, 50, 0, 90)
        linegrad2.addColorStop(0.5, '#000')
        linegrad2.addColorStop(1, 'rgba(0,0,0,0)')




        ctx.fillStyle = linegrad;
        ctx.fillRect(10, 10, 200, 150);

        ctx.strokeStyle = linegrad2;
        ctx.strokeRect(50, 50, 100, 100)



      } else {
        alert('你的浏览器太low了~')
      }
    }
  </script>

</body>

</html>